package game.gui;

import engine.game.Manager;
import engine.graphics.Color4f;
import engine.graphics.FontManager;
import engine.gui.Grid;
import engine.gui.GridField;
import engine.gui.ParentObject;
import engine.gui.events.EventMethod;
import engine.objects.ObjectInspector;
import game.objects.nonstaticObjects.ItemModel;

public class InventoryGrid extends Grid {
	
	private static final int ROWS = 4,
							 COLS = 7,
							 CELL_SPACING = 4, 
							 PADDING_X = 25, 
							 PADDING_Y = 25 ; 

	private static final long serialVersionUID = 1L;

	private ContextMenu contextMenu ; 
	protected int selFieldID = 0 ; 
	private InventoryGridField selField = null ; 
	
	public InventoryGrid(Manager m, ParentObject parent, int w, int h,
			int cols, int rows) {
		super(m, parent, w, h, cols, rows);
	}

	public InventoryGrid(Manager m, ParentObject parent, int w, int h,
			int cols, int rows, int cellspacing) {
		super(m, parent, w, h, cols, rows, cellspacing);
	}

	public InventoryGrid(Manager manager, ParentObject parent, int w, int h) {
		this(manager, parent, w-PADDING_X*2, h-PADDING_Y*2, ROWS, COLS, CELL_SPACING);
		this.padding(PADDING_X, PADDING_Y); 
	}

	public void addNewItem(ItemModel item){
		if(this.hasEmptyBox()){
			InventoryGridField f = (InventoryGridField) this.getFirstEmptyBox();
			f.addNewItem(item);
		}else
			System.out.println("There's no free slot available in the inventory!");
	}
		
	public void update(){
		super.update(); 
		for (GridField[] f1: this.boxes){
			for(GridField f2: f1){
				InventoryGridField tmp = (InventoryGridField)f2 ;
				if(tmp.isSelected() && tmp.getID() == this.selFieldID)
					this.selField = tmp ; 
				else
					tmp.deselect(); 
			}
		}
		if(this.selField == null || !this.selField.hasContent())
			this.contextMenu.hide(); 
		else{
			if(selField.getItem().getItem().hasDirectEffect() && this.contextMenu.getChildByName("Use").isVisible())
				this.contextMenu.getChildByName("Use").hide(); 
			else if(!this.contextMenu.getChildByName("Use").isVisible())
				this.contextMenu.getChildByName("Use").show();
		}
	}
	
	@SuppressWarnings("serial")
	protected void initFields(){
		if(cols == 0 || rows == 0) return ; 
		boxes = new GridField[cols][rows];
		int bw = this.getObjectWidth() / cols - cellspacing / 2;
		int bh = this.getObjectHeight() / rows - cellspacing / 2;
		int id = 0 ; 
		for (int x = 0; x < cols; x++) {
			for (int y = 0; y < rows; y++) {
				InventoryGridField tmp = new InventoryGridField(this.getManager(), this, bw, bh, id);
				tmp.padding(x*(bw+cellspacing/2), y*(bh+cellspacing/2)); 
				tmp.setFontName(FontManager.S_FONT_13); 
				boxes[x][y] = tmp ; 
				this.addNewChild("box("+x+"|"+y+")", boxes[x][y]);
				id++ ; 
			}
		}
		contextMenu = new ContextMenu(this.getManager(), (int)this.getMaxX()-100, 
						(int)this.getMinY()-40, 100, 40) ;
		this.contextMenu.changeNormalColor(new Color4f(1,1,1,1));
		this.contextMenu.addNewButton("Use", new EventMethod(){
			public void run(){
				if(selField != null && selField.hasContent()){
					ItemModel i = (ItemModel)ObjectInspector.getInstance(selField.getItem(), ItemModel.class); 
					if(!i.getItem().hasDirectEffect()){
						i.getItem().releaseEffectOn(i.wasPickedUpBy().getEntity());
						selField.removeContent() ; 
					}
				}
				this.interrupt(); 
			}
		});
		this.contextMenu.addNewButton("Drop", new EventMethod(){
			public void run(){
				if(selField != null && selField.hasContent()){
					selField.removeContent(); 
				}
				this.interrupt(); 
			}
		});
		this.addNewChild("contextMenu", contextMenu);
	}
}
